﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class BasicCard : MonoBehaviour
{
    public bool isOver;
    public DeckType belongDeckType = DeckType.Action;
    public GameObject targetGrid;

    public CardPair pairData;
    private CardScriptable _cardData;
    BattleResolver battleResolver;

    GameObject receiver;
    Vector3 lastMousePosition;

    private Transform m_transform;

    public BaseEntity caller;
    
    private void Awake()
    {
        targetGrid = GameObject.FindGameObjectWithTag(StaticDataManager.TARGET_GRID_TAG);
        
        // TODO BR应该是全局的
        try
        {
            battleResolver = GameObject.FindGameObjectWithTag(StaticDataManager.BATTLE_RESOLVER_TAG).GetComponent<BattleResolver>();
        }
        catch (Exception e)
        {
        }


        m_transform = transform;
    }

    public void SetPairData(CardPair cardPair)
    {
        this.pairData = cardPair;
        if (this.pairData != null)
        {
            this._cardData = this.pairData.cardData;
        }
    }
    
    [Obsolete]
    public bool ExecuteCard()
    {
        // TODO 先判断点数够不够
        // TODO 然后设置执行者
        // TODO 然后判断接受者是否存在(范围之内)
        // TODO 然后转交效果给BR
        // TODO 返回BR的执行结果
        if (belongDeckType == DeckType.Action)
        {
            if (caller._ap < _cardData.cost)
            {
                Debug.Log("AP 点数不够");
                return false;
            }
        }
        else
        {
            if (caller._fp < _cardData.cost)
            {
                Debug.Log("FP 点数不够");
                return false;
            } 
        }

        string receiverTag;
        
        if (_cardData.targetType == TargetType.Self)
        {
            receiver = GameObject.FindGameObjectWithTag(StaticDataManager.PLAYER_TAG);
            receiverTag = StaticDataManager.PLAYER_LAYER;
        }
        else
        {
            receiverTag = StaticDataManager.ENEMY_LAYER;
        }
        
        // 先判断 TargetObject是否存在
        if (!receiver) return false;
        
        // 然后判断 TargetObject 是否在攻击范围内
        if (!IsReceiverInRange(caller.gameObject, receiver, receiverTag))
        {
            Debug.Log("范围不够");
            return false;
        }
        
        if (BattleResolver.DealWithCardEffect(caller.gameObject, receiver, _cardData, Vector3.zero))
        {
            // 然后开始计算消耗
            if (belongDeckType == DeckType.Action)
            {
                caller.APChange(-_cardData.cost);
                Debug.Log($"当前的 AP 点数是 {caller._ap}");
            }
            else
            {
                caller.FPChange(-_cardData.cost);
                Debug.Log($"当前的 FP 点数是 {caller._fp}");
            }
            
            // TODO 然后把自己扔回到回收区，并且隐藏当前卡牌
            // TODO 不应当 只能返回是或者否
            // TODO 卡牌只是卡牌 他自己没用其他东西 都是靠外力的
            return true;
        }

        return false;
    }
    
    [Obsolete]
    private bool IsReceiverInRange(GameObject caller, GameObject receiver, String layerName)
    {
        Collider2D[] hits = TileManager.GetRangeCollider2Ds(caller.transform.position, _cardData.range, _cardData.rangeType, layerName);

        foreach (Collider2D hit in hits)
        {
            if (hit.gameObject == receiver)
            {
                return true;
            }
        }
        
        return false;
    }

    #region 工具函数
    protected void ShowTargetGridAtMousePosition()
    {
        // 显示指针位置
        if (targetGrid.transform.localScale != Vector3.one)
        {
            targetGrid.transform.localScale = Vector3.one;
        }
        Vector3 mousePosition = Input.mousePosition;
        mousePosition.z = 20;

        mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

        targetGrid.transform.position = mousePosition;

        // 通过 raycast 查看当前的坐标上是否有可以打击的角色
        Collider2D[] colliders = Physics2D.OverlapPointAll(mousePosition);

        bool isTargetFound = false;

        foreach (Collider2D item in colliders)
        {
            if (!item) continue;
            BaseEntity entity = item.GetComponent<BaseEntity>();
            
            if (!entity) continue;
            
            receiver = item.gameObject;
            isTargetFound = true;
            lastMousePosition = mousePosition;
            
            break;
        }

        if (!isTargetFound)
        {
            targetGrid.GetComponent<TargetLockGridBehaviour>().UnlockTarget();
            receiver = null;
            lastMousePosition = Vector3.zero;
        }
        else
        {
            // 为悬浮到的对象设置一个锁定框
            targetGrid.GetComponent<TargetLockGridBehaviour>().LockTarget(receiver);
        }
    }
    

    protected void HideTargetGrid()
    {
        targetGrid.transform.localScale = Vector3.zero;
    }

    #endregion
}
